;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.LogisticStatusDisplayBoard", [])
        .controller("LogisticStatusDisplayBoardCtrl", ["$scope", "ybHttp", "ybWidget", "MESG_TYPE", "$filter", "$timeout", function ($scope, ybHttp, ybWidget, MESG_TYPE, $filter, $timeout) {
            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        name: "序号",
                        dataField: "noC",
                        width: 60,
                        isShow: true,
                    },
                    {
                        name: "物流运单号",
                        dataField: "wayBillNo",
                        width: 180,
                        isShow: true,
                    },
                    {
                        name: "客户名称",
                        dataField: "dealerOrgName",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "发货方式",
                        dataField: "shippingMethod",
                        width: 100,
                        isShow: true,
                    },
                    {
                        name: "承运商",
                        dataField: "carrierOrgName",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "物流状态",
                        dataField: "logisticStatusShow",
                        width: 80,
                        isShow: true,
                    },
                    {
                        name: "发货日期",
                        dataField: "deliveryTimeShow",
                        width: 140,
                        isShow: true,
                    },
                    {
                        name: "更新日期",
                        dataField: "updateTimeShow",
                        width: 140,
                        isShow: true,
                    },
                    {
                        name: "当前物流信息",
                        dataField: "nodeInfo",
                        width: 250,
                        isShow: true,
                    },
                ]
            };


            $scope.pageInfo = {
                totalPages: '',
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.search = function () {
                if (!$scope.intervalFlag) {
                    // 没有定时器时，重启定时器
                    $scope.pageInfo.totalPages = '';
                    startInterval()
                }
                getDataList($scope.pageInfo.curPage);
            };


            $scope.dataList = [];
            $scope.currentPage = '';

            function getDataList(val) {
                if ($scope.pageInfo.totalPages == 1) {
                    // 如果只有一页，清除定时器
                    closeInterval();
                    return;
                }

                if ($scope.currentPage == $scope.pageInfo.totalPages) {
                    // 查询最后一页后，再从第一页循环查询数据
                    $scope.currentPage = 1;
                } else {
                    // 自动翻页
                    $scope.currentPage ++;
                }

                if (val) {
                    // 如果点击了分页组件，按分页组件的页数查询
                    $scope.currentPage = val;
                }

                $scope.pageInfo.curPage = $scope.currentPage;

                var service = "device";
                var reqUrl = "/logisticStatus/logisticStatusDisplayBoard";
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize
                };
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.pageInfoList, function (item, index) {
                        item.noC = ($scope.pageInfo.curPage-1) * $scope.pageInfo.pageSize + index + 1;
                        item.logisticStatusShow = $filter("ProgressStatus")(item.logisticStatus, "logisticStatus");
                        item.deliveryTimeShow = $filter('YbDate')(item.deliveryTime, "yyyy-MM-dd HH:mm:ss");
                        item.updateTimeShow = $filter('YbDate')(item.updateTime, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.dataList = resp.value.pageInfoList;

                    $scope.pageInfo.totalRows = resp.value.pageInfo.total;
                    $scope.pageInfo.totalPages = resp.value.pageInfo.pages;
                });
            }

            $scope.$on("$destroy", function () {
                closeInterval()
            });

            function startInterval() {
                $scope.intervalFlag = setInterval(getDataList, 30000)
            }

            function closeInterval() {
                clearInterval($scope.intervalFlag);
                $scope.intervalFlag = null;
            }

            (function () {
                getDataList()
                startInterval()
            })();
        }]);
})(window, window.angular);